package com.ftg.learn.chapter02.javabase;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 零配置 -
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/12/26 上午8:50
 */
public class ZeroConfiguration {

    //6个原则  : 单一职责（只有一个功能） 里氏代替(多态)  开闭原则()   合成复用   依赖倒置  接口隔离    迪米特法则(最少知道原则)

    public static void main(String[] args) {
        Configuration config = new Configuration();

        config.setLogImpl(StdOutImpl.class);

        TransactionFactory transactionFactory = new JdbcTransactionFactory();

        //1
        MysqlDataSource datasource = new MysqlDataSource();
        datasource.setURL("jdbc:mysql://localhost:10086/j2309");
        datasource.setUser("root");
        datasource.setPassword("123456");

        Environment e = new Environment("demo01",transactionFactory,datasource);
        config.setEnvironment(e);

        // 反射 第一种
//        类名。Class
//        Class.forName(类地址)
//        实例点.getClass();

//        加载xml
        String path = "/home/kangjx/userpanel/workspace/java/learnjava2309/learnjava2309/src/main/resources/zero/sysuser.xml";
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);

            // 使用 XMLMapperBuilder 注入映射文件
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, config, "sss", null);
            xmlMapperBuilder.parse();

        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        }


//        表示把一个纯java的接口注入到mybatis
//        config.addMapper(Isysuser.class);

        System.out.println("是不是我们用java把配置文件去掉了");

        //生成工厂
        SqlSessionFactory sqlsessionFactory = new SqlSessionFactoryBuilder().build(config);

        //生成sqlsession缓存
        SqlSession sqlSession = sqlsessionFactory.openSession();

        //通过缓存取到Isysuser mapper
        Isysuser isysuser = sqlSession.getMapper(Isysuser.class);


//        int num = isysuser.insert(new User("admin123456","123456"));
//        System.out.println(num);


//        int num1 = isysuser.modify(new User(4,"lcq","lcq"));
//
//        System.out.println(num1);

//        int num2 = isysuser.modify1(new User("lzy","lzy"),4);
//        System.out.println(num2);
//        isysuser.deleteById(8);

//        User u = new User();
////        u.setId(4);
//        u.setUsername("lcq");
//
//        int num = isysuser.modifyUser(u);
//
//        System.out.println(num);


        Scanner a = new Scanner(System.in);
        Integer id =new Integer( a.next());
        if(id != null){
            isysuser.deleteUserById(id);
        }


        //异常  ·「可控异常 不可控异常 」

//        isysuser.queryAllWithParameter2("刘",null).forEach(System.out::println);



        sqlSession.commit();
        sqlSession.close();
    }

}
